package com.zjitc.controller;

import com.zjitc.entity.Product;
import com.zjitc.service.EmailService;
import com.zjitc.service.ProductService;
import com.zjitc.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 邮件发送控制器
 */
@RestController
@RequestMapping("/email")
//@PreAuthorize("hasAuthority('product:admin')")
public class EmailController {

    @Autowired
    private EmailService emailService;
    
    @Autowired
    private ProductService productService;
//
//    /**
//     * 发送产品信息邮件
//     *
//     * @param to 收件人邮箱
//     * @param productId 产品ID
//     * @return 操作结果
//     */
//    @PostMapping("/send-product")
//    public ResponseResult<String> sendProductEmail(@RequestParam String to,
//                                                @RequestParam Integer productId,
//                                                @RequestParam(required = false) String recipientName) {
//        try {
//            // 查询产品信息
//            Product product = productService.findProductById(productId);
//            if (product == null) {
//                return ResponseResult.error("产品不存在");
//            }
//
//            // 准备邮件模板变量
//            Map<String, Object> variables = new HashMap<>();
//            variables.put("title", "产品信息通知");
//            variables.put("recipientName", recipientName != null ? recipientName : "用户");
//            variables.put("message", "以下是您关注的产品信息详情：");
//            variables.put("product", product);
//
//            // 发送邮件
//            emailService.sendTemplateEmail(to, "产品信息 - " + product.getName(), "email-template", variables);
//
//            return ResponseResult.success("邮件发送成功");
//        } catch (Exception e) {
//            return ResponseResult.error("邮件发送失败：" + e.getMessage());
//        }
//    }
//
//    /**
//     * 发送自定义邮件
//     *
//     * @param to 收件人邮箱
//     * @param subject 邮件主题
//     * @param recipientName 收件人姓名
//     * @param message 自定义消息
//     * @return 操作结果
//     */
//    @PostMapping("/send-custom")
//    public ResponseResult<String> sendCustomEmail(@RequestParam String to,
//                                               @RequestParam String subject,
//                                               @RequestParam(required = false) String recipientName,
//                                               @RequestParam String message) {
//        try {
//            // 准备邮件模板变量
//            Map<String, Object> variables = new HashMap<>();
//            variables.put("title", subject);
//            variables.put("recipientName", recipientName != null ? recipientName : "用户");
//            variables.put("message", message);
//
//            // 发送邮件
//            emailService.sendTemplateEmail(to, subject, "email-template", variables);
//
//            return ResponseResult.success("邮件发送成功");
//        } catch (Exception e) {
//            return ResponseResult.error("邮件发送失败：" + e.getMessage());
//        }
//    }
//    简单的模板邮件发送示例：
//    @PostMapping("/send-simple")
    @PostMapping("simple")
    public ResponseResult<String> sendSimpleEmail(@RequestParam String to,
                                                  @RequestParam String subject,
                                                  @RequestParam String content) {
        try {
            emailService.sendSimpleEmail(to, subject, content);
            return ResponseResult.success("邮件发送成功");
        } catch (Exception e) {
            return ResponseResult.error("邮件发送失败：" + e.getMessage());
        }
    }
    //sendTemplateEmail
    @PostMapping("template")
    public ResponseResult<String> sendTemplateEmail(@RequestParam String to,
                                                  @RequestParam String subject,
                                                  @RequestParam String templateName,
                                                  @RequestParam(required = false) String title,
                                                  @RequestParam(required = false) String recipientName,
                                                  @RequestParam(required = false) String message,
                                                  @RequestParam(required = false) Map<String, Object> variables) {
        try {
            // 添加日志输出接收到的变量
            System.out.println("收到模板邮件请求：");
            System.out.println("to: " + to);
            System.out.println("subject: " + subject);
            System.out.println("templateName: " + templateName);
            
            // 创建一个新的变量Map
            Map<String, Object> templateVars = new HashMap<>();
            
            // 如果提供了单独的参数，优先使用这些参数
            if (title != null) {
                templateVars.put("title", title);
            }
            if (recipientName != null) {
                templateVars.put("recipientName", recipientName);
            }
            if (message != null) {
                templateVars.put("message", message);
            }
            
            // 如果还提供了variables参数，合并这些变量
            if (variables != null && !variables.isEmpty()) {
                System.out.println("合并variables参数: " + variables);
                templateVars.putAll(variables);
            }
            
            // 如果没有提供标题，使用subject作为默认值
            if (!templateVars.containsKey("title")) {
                templateVars.put("title", subject);
            }
            
            // 如果仍然缺少必要的变量，添加默认值
            if (!templateVars.containsKey("recipientName")) {
                templateVars.put("recipientName", "用户");
            }
            if (!templateVars.containsKey("message")) {
                templateVars.put("message", "这是一封系统邮件");
            }
            
            System.out.println("最终使用的模板变量: " + templateVars);
            
            emailService.sendTemplateEmail(to, subject, templateName, templateVars);
            return ResponseResult.success("邮件发送成功");
            }
         catch (Exception e) {
            System.out.println("邮件发送失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseResult.error("邮件发送失败：" + e.getMessage());
            }
         }

    /**
     * 直接接收各个参数的方法，避免使用variables[key]格式
     */
    @PostMapping("template2")
    public ResponseResult<String> sendDirectEmail(@RequestParam String to,
                                             @RequestParam String subject,
                                             @RequestParam(required = false, defaultValue = "邮件通知") String title,
                                             @RequestParam(required = false, defaultValue = "用户") String recipientName,
                                             @RequestParam(required = false, defaultValue = "这是一封系统邮件") String message) {
        try {
            System.out.println("\n===== 直接处理模板邮件 =====");
            System.out.println("to: " + to);
            System.out.println("subject: " + subject);
            System.out.println("title: " + title);
            System.out.println("recipientName: " + recipientName);
            System.out.println("message: " + message);
            
            // 创建变量Map
            Map<String, Object> templateVars = new HashMap<>();
            templateVars.put("title", title);
            templateVars.put("recipientName", recipientName);
            templateVars.put("message", message);
            
            // 使用模板发送邮件
            emailService.sendTemplateEmail(to, subject, "email-template", templateVars);
            return ResponseResult.success("邮件发送成功");
        } catch (Exception e) {
            System.out.println("邮件发送失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseResult.error("邮件发送失败：" + e.getMessage());
        }
    }
} 